package com.devilvan.consumer.controller;

import com.devilvan.entity.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping("consumer")
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    /**
     * 由于我们请求数据的服务在8001端口，所以restful的网络前缀就提出来，写成：
     * http://localhost:8001/
     */
//    private static final String REST_URL_PREFIX = "http://localhost:8001";

    // 使用提供者已发布的服务，而不是固定的端口号。 
    private static final String REST_URL_PREFIX = "http://springcloud-provide-dept";


    @GetMapping(value = "add",produces = "application/json; charset=UTF-8")
    public boolean add(Dept dept) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/addDept", Boolean.class, dept);
    }

    @GetMapping(value = "get/{dept_id}",produces = "application/json; charset=UTF-8")
    public Dept get(@RequestBody@PathVariable("dept_id") Long dept_id) {
        return restTemplate.getForObject(REST_URL_PREFIX + "/queryDeptById/" + dept_id, Dept.class);
    }

    @GetMapping(value = "get",produces = "application/json; charset=UTF-8")
    public List<Dept> get() {
        return restTemplate.getForObject(REST_URL_PREFIX + "/queryAllDepts", List.class);
    }
}
